// 格式化日期：yyyy-MM-dd
function formatDate(date) {
  var myyear = date.getFullYear()
  var mymonth = date.getMonth() + 1
  var myweekday = date.getDate()

  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  return (myyear + '-' + mymonth + '-' + myweekday)
}
export function timestampToTime(timestamp) {
  var date = new Date(timestamp)
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
  var h = date.getHours() + ':'
  var m = date.getMinutes() + ':'
  var s = date.getSeconds()
  return Y + M + D + h + m + s
}
// 获取本周开始结束时间
export function getCurrentWeekTimeInfo() {
  var arr = []
  var now = new Date() // 当前日期
  var nowDayOfWeek = now.getDay() // 今天本周的第几天
  var nowDay = now.getDate() // 当前日
  var nowMonth = now.getMonth() // 当前月
  var nowYear = now.getFullYear() // 当前年
  nowDayOfWeek = nowDayOfWeek === 0 ? 6 : nowDayOfWeek
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1)
  arr.push(formatDate(weekStartDate) + ' 00:00:00')
  var weekEndDate = new Date()
  arr.push(formatDate(weekEndDate) + ' 23:59:59')
  return arr
}
// 获取上周开始结束时间
export function getLastWeekTimeInfo() {
  var arr = []
  var now = new Date() // 当前日期
  var nowDayOfWeek = now.getDay() // 今天本周的第几天
  var nowDay = now.getDate() // 当前日
  var nowMonth = now.getMonth() // 当前月
  var nowYear = now.getFullYear() // 当前年
  nowDayOfWeek = nowDayOfWeek === 0 ? 6 : nowDayOfWeek
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7 + 1)
  arr.push(formatDate(weekStartDate) + ' 00:00:00')
  var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
  arr.push(formatDate(weekEndDate) + ' 23:59:59')
  return arr
}
// 获取上月开始结束时间
export function getLastMonthTimeInfo() {
  var arr = []
  var now = new Date() // 当前日期
  var nowMonth = now.getMonth() - 1 // 当前月
  now.setMonth(nowMonth - 1)
  var nowYear = now.getFullYear() // 当前年
  var monthStartDate = new Date(nowYear, nowMonth, 1)
  arr.push(formatDate(monthStartDate) + ' 00:00:00')
  var monthEndDate = new Date(monthStartDate.getFullYear(), monthStartDate.getMonth(), getMonthDays(monthStartDate.getMonth()))
  arr.push(formatDate(monthEndDate) + ' 23:59:59')
  return arr
}
// 获得某月的天数 可以
function getMonthDays(myMonth) {
  var now = new Date() // 当前日期
  var nowYear = now.getFullYear() // 当前年
  var monthStartDate = new Date(nowYear, myMonth, 1)
  var monthEndDate = new Date(nowYear, myMonth + 1, 1)
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
  return days
}
// 获取本月开始结束时间
export function getCurrentMonthTimeInfo() {
  var arr = []
  var now = new Date() // 当前日期
  var nowMonth = now.getMonth() // 当前月
  var nowYear = now.getFullYear() // 当前年
  var monthStartDate = new Date(nowYear, nowMonth, 1)
  arr.push(formatDate(monthStartDate) + ' 00:00:00')
  var monthEndDate = new Date()
  arr.push(formatDate(monthEndDate) + ' 23:59:59')
  return arr
}
// 获取当天开始结束时间
export function getCurrentDayTimeInfo() {
  var arr = []
  var today = new Date()
  arr.push(formatDate(today) + ' 00:00:00')
  arr.push(formatDate(today) + ' 23:59:59')
  return arr
}

// 获取昨天开始结束时间
export function getYesterDayTimeInfo() {
  var arr = []
  var today = new Date()
  var yesterday = new Date(today.setTime(today.getTime() - 24 * 60 * 60 * 1000))
  arr.push(formatDate(yesterday) + ' 00:00:00')
  arr.push(formatDate(yesterday) + ' 23:59:59')
  return arr
}
